package k2_oop.p9_collection.demo02_01;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Magic {

    Random random = new Random();

    public final String[] PLAYINGCARDNUMBER = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    public final String[] PLAYINGCARDFLOWER = {"红桃","黑桃","梅花","方片"};

    public final String TITLE = "见证奇迹的时刻";

    //全局用来存储卡牌的集合
    public List<String> playingCard = new ArrayList<>();

    //初始化一个终止抽取卡牌的标识
    int stopFlag = 0;
    //初始化当前被抽取的卡牌
    String currentCard = "";

    //用来初始化8张卡牌的匿名块
    {
        //第一个步骤：随机拿出4张牌
        System.out.println("=====随机抽取的4张牌=====");
        while(stopFlag<4){
            //抽取一张卡牌
            currentCard = PLAYINGCARDFLOWER[random.nextInt(PLAYINGCARDFLOWER.length)]+
                    PLAYINGCARDNUMBER[random.nextInt(PLAYINGCARDNUMBER.length)];
            //如果当前被抽取的卡牌不存在，可以添加到集合中
            if(!playingCard.contains(currentCard)){
                playingCard.add(currentCard);
                stopFlag++;
            }
        }
        System.out.println(playingCard);

        //第二个步骤：把4张牌对半撕开，堆在一起
        System.out.println("=====撕开后堆在一起的8张牌=====");
        playingCard.addAll(playingCard.stream()
                .map(card->card+"+")
                .collect(Collectors.toList()));
        System.out.println(playingCard);
    }

    //全局的变量，表示此时此刻剩余卡牌的数量，初始应该是8张卡牌
    public int CARDCOUNT = playingCard.size();


    /**
     * 魔术解密的过程
     * @param name
     * @param position
     * @param gender
     */
    public void decode(String name,String position,String gender){

        //第三个步骤：根据名字的字数往下放牌，每说一个字就把第一张牌放到最下面
        System.out.println("======你的名字是"+name+",长度是"+name.length()+",往下放"+name.length()+"牌后的8张牌====");
        this.swapCard(name.length());

        System.out.println(playingCard);

        //第四个步骤：拿起最上面的三张牌并插到中间
        System.out.println("=====最上面的三张牌插到中间后的8张牌=====");
        this.insertCardToMiddle(3);

        System.out.println(playingCard);

        //第五个步骤：把最上面的一张牌藏起来
        String kingCard = playingCard.remove(0);
        System.out.println("=====被藏起来的第一张牌=====\n"+kingCard);
        CARDCOUNT--;
        System.out.println("=====当前剩余的7张卡牌=====\n"+playingCard);

        //第六个步骤：把最上面的1张（南方人）或2张（北方人）或3张（不确定）插到中间
        switch (position){
            case "Y":
                System.out.println("=====你是南方人,往中间插入一张牌后的7张牌=====");
                this.insertCardToMiddle(1);
                break;
            case "N":
                System.out.println("=====你是北方人,往中间插入两张牌后的7张牌=====");
                this.insertCardToMiddle(2);
                break;
            case "U":
                System.out.println("=====你是中间人,往中间插入三张牌后的7张牌=====");
                this.insertCardToMiddle(3);
                break;
            default:
                System.out.println("没有这个选项");
        }
        System.out.println(playingCard);

        //第七个步骤：扔掉最上面的一张牌（男），或者最上面的两张牌（女）
        int removeCardCount = 0;
        switch (gender){
            case "Y":
                System.out.println("=====你是小哥哥,扔掉一张牌后的6张牌=====");
                removeCardCount=1;
                break;
            case "N":
                System.out.println("=====你是小姐姐,扔掉两张牌后的5张牌=====");
                removeCardCount = 2;
                break;
            default:
                System.out.println("没有这个选项");
        }
        //@TODO 可以采取这种方式，也可以直接在 Switch 中直接删除
        for (int i = 0; i < removeCardCount; i++) {
            playingCard.remove(0);
            CARDCOUNT--;
        }

        System.out.println(playingCard);

        //第八个步骤:“见证奇迹的时刻”洗牌，每说一个字就把第一张牌放到最下面
        System.out.println("=====见证奇迹的时刻=====");
        this.swapCard(TITLE.length());
        System.out.println(playingCard);

        //第九个步骤：“好运留下来，烦恼丢出去”，直到留下最后一张
        while(CARDCOUNT>1){
            this.swapCard(1);
            System.out.println("=====好运留下来=====\n"+playingCard);
            playingCard.remove(0);
            CARDCOUNT--;
            System.out.println("=====烦恼丢出去=====\n"+playingCard);
        }

        //第十个步骤：判断被藏起来的卡牌和剩余的卡牌是不是同一张卡牌
        String remainCard = playingCard.get(0);
        if(remainCard.replace("+","").equals(kingCard.replace("+",""))){
            System.out.println("成功了");
        }else{
            System.out.println("失败了");
        }
    }

    /**
     * 将最上面的 n 张卡牌依次切换到最下面
     * @param swapCount 切换的卡牌数量
     */
    public void swapCard(int swapCount){
        int actualCount = swapCount % CARDCOUNT;
        playingCard.addAll(playingCard.subList(0,actualCount));

        playingCard = playingCard.subList(actualCount,playingCard.size());
    }


    /**
     * 将前n张卡牌插入到剩余卡牌的中间
     * @param count 需要插入的数量
     */
    public void insertCardToMiddle(int count){

        //获取要插入中间的那些卡片
        List<String> toInsertCards = playingCard.subList(0,count);
        //随机找出一个可以插入卡牌的索引位置
        int toInsertIndex = random.nextInt(CARDCOUNT-count-1)+count+1;
        //向指定位置插入卡牌
        playingCard.addAll(toInsertIndex,toInsertCards);

        playingCard = playingCard.subList(count, playingCard.size());

    }

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        Magic magic = new Magic();

        System.out.println("你叫什么名字?");
        String name = scanner.nextLine();

        System.out.println("你是南方小土豆吗[Y/N/U]");
        String position = scanner.nextLine().toUpperCase();

        System.out.println("你是纯爷们吗[Y/N]");
        String gender = scanner.nextLine().toUpperCase();

        magic.decode(name,position,gender);
    }
}
